﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Ledict.ConditionExtract.DAL;
using Ledict.ConditionExtract.Model;
using System.Text.RegularExpressions;

namespace Ledict.ConditionExtract.BLL
{
    /// <summary>
    /// 引用法规BLL
    /// </summary>
    public class QuoteLawRelBLL
    {
        QuoteLawRelDAL dal = new QuoteLawRelDAL();
        CaseCauseDAL casecausedal = new CaseCauseDAL();
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataTable QuoteLawRelGetList(string SituationID)
        {
            return dal.QuoteLawRelGetList(SituationID);
        }

        /// <summary>
        /// 获得数据列表(程序法)
        /// </summary>
        /// <param name="caseTypeId"></param>
        /// <returns></returns>
        public DataTable QuoteLawRelByProceduralLaw(string caseTypeId)
        {
            return dal.QuoteLawRelByProceduralLaw(caseTypeId);
        }

        /// <summary>
        /// 获得数据列表(组合)
        /// </summary>
        /// <param name="caseTypeId"></param>
        /// <returns></returns>
        public DataTable QuoteLawRelByCombiationLaw(string caseTypeId)
        {
            return dal.QuoteLawRelByCombiationLaw(caseTypeId);
        }

        /// <summary>
        /// 获得数据列表(民事)
        /// </summary>
        public DataTable QuoteLawRelGetListByMinShi(string situationId)//where OLR_SituationID=@SituationID 
        {
            return dal.QuoteLawRelGetListByMinShi(situationId);
        }

        public string GetHeadwordInID(string ccId)
        {
            return dal.GetHeadwordInID(ccId);
        }

        public string QuoteLawRelGetProv(string SituationID)
        {
            return dal.QuoteLawRelGetProv(SituationID);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(SYS_QuoteLawRel Model)
        {
            return dal.Add(Model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string QLR_ID)
        {
            return dal.Delete(QLR_ID);
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public bool Update(SYS_QuoteLawRel Model)
        {
            return dal.Update(Model);
        }

        /// <summary>
        /// 获取法规效力等级编号和名称
        /// </summary>
        /// <returns></returns>
        public List<SYS_EffectiveGrade> GetHeadWindows()
        {
            return dal.GetHeadWindows();
        }

        /// <summary>
        /// 得到某节点的相关法条
        /// </summary>
        /// <param name="SituationID"></param>
        /// <param name="SituationName"></param>
        /// <param name="SituationType"></param>
        /// <param name="Pid"></param>
        /// <param name="isheadOrCause">1是节点树 其他是案由树</param>
        /// <returns></returns>
        public DataTable QuoteLawRelAboutCaseList(string SituationID, string SituationName, string SituationType, string Pid, int isheadOrCause)//
        {
            DataTable Resultdt = new DataTable();
            DataTable pdt = new DataTable();
            if (SituationType != "2" && isheadOrCause == 1)//不是案由
            {
                Resultdt = QuoteLawRelByType(SituationID, SituationName, SituationType, isheadOrCause);
                while (Resultdt.Rows.Count == 0)
                {
                    if (!string.IsNullOrEmpty(Pid))//没父级
                    {
                        pdt = casecausedal.GetSYS_CaseCauseByCCID(Pid);// 取父级
                        if (pdt.Rows.Count > 0)
                        {
                            Resultdt = QuoteLawRelByType(Pid, pdt.Rows[0]["CC_Name"].ToString(), pdt.Rows[0]["CC_NodeType"].ToString(), isheadOrCause);
                            Pid = pdt.Rows[0]["CC_ParentID"].ToString();
                        }
                        else
                        {
                            return new DataTable();
                        }
                    }
                    else
                    {
                        return new DataTable();
                    }
                }
            }
            else
            {
                Resultdt = QuoteLawRelByType(SituationID, SituationName, SituationType, isheadOrCause);
            }
            return Resultdt;
        }
        /// <summary>
        /// 某节点的法条ID 
        /// </summary>
        /// <param name="SituationID"></param>
        /// <param name="SituationName"></param>
        /// <param name="SituationType"></param>
        /// <param name="isheadOrCause"></param>
        /// <returns></returns>
        public DataTable QuoteLawRelByType(string SituationID, string SituationName, string SituationType, int isheadOrCause)
        {
            DataTable Resultdt = new DataTable();
            if (SituationType != "2" && isheadOrCause == 1)
            {
                Resultdt = dal.GetRelProvId(SituationID, SituationName, 0); //名字转ID
            }
            else
            {
                Resultdt = dal.GetRelProvId(SituationID, SituationName, 1);
            }
            return Resultdt;
        }


        public DataTable GetAllByWhere(string where)
        {

            return dal.GetAllByWhere(where);
        }

        /// <summary>
        /// 获得条文的具体属性内容
        /// </summary>
        ///  
        ///  
        public DataTable QuoteLawRel_ProvContent(DataTable dt, string stiuationId)
        {
            List<int> deleteL = new List<int>();
            dt.Columns.Add("CATInID", typeof(string));
            dt.Columns.Add("CATName", typeof(string));
            dt.Columns.Add("PromuDate", typeof(string));
            dt.Columns.Add("HWType", typeof(string));
            dt.Columns.Add("HWName", typeof(string));
            dt.Columns.Add("LostFlag", typeof(string));
            dt.Columns.Add("isAdd", typeof(string));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataTable ProvDt = dal.GetProvContent(dt.Rows[i]["ProvInID"].ToString());
                if (ProvDt.Rows.Count > 0)//条文表LawProv 条文不会重复
                {
                    dt.Rows[i]["CATInID"] = ProvDt.Rows[0]["CATInID"].ToString();
                    dt.Rows[i]["CATName"] = ProvDt.Rows[0]["CATName"].ToString();
                    dt.Rows[i]["PromuDate"] = ProvDt.Rows[0]["PromuDate"].ToString();
                    dt.Rows[i]["HWType"] = ProvDt.Rows[0]["HWType"].ToString();
                    dt.Rows[i]["HWName"] = ProvDt.Rows[0]["HWName"].ToString();
                    dt.Rows[i]["LostFlag"] = ProvDt.Rows[0]["LostFlag"].ToString();
                    if (dt.Rows[i]["ProvNumber"].ToString() != "0" && dt.Rows[i]["ProvNumber"].ToString() != "-1" && dt.Rows[i]["ProvNumber"].ToString() != "-2")
                    {
                        dt.Rows[i]["ProvInName"] = "第" + dt.Rows[i]["ProvNumber"] + "条";
                    }
                    else if (dt.Rows[i]["ProvNumber"].ToString() == "-2")
                    {
                        dt.Rows[i]["ProvInName"] = System.Text.RegularExpressions.Regex.Replace(dt.Rows[i]["ProvInName"].ToString().Trim(), @".*?(?=[\d一二三四五六七八九十百千万零]+?条)", "");
                        dt.Rows[i]["ProvInName"] = "第" + dt.Rows[i]["ProvInName"];
                    }

                    dt.Rows[i]["ProvInName"] = dt.Rows[i]["ProvInName"].ToString().Trim().Replace("第一十", "第十");

                    if (dal.isAdd(dt.Rows[i]["ProvInID"].ToString(), stiuationId) != "0")
                    {
                        dt.Rows[i]["isAdd"] = "已添加";
                    }
                }
                else
                {
                    deleteL.Add(i);

                }

            }
            foreach (int item in deleteL)
            {
                dt.Rows.Remove(dt.Rows[item]);
            }


            return dt;


        }

        /// <summary>
        /// 获取法规添加法规信息(查询)
        /// </summary>
        /// <param name="condition">查询法律法规条件</param>
        /// <returns>返回法律法规查询用界面信息</returns>
        public DataTable GetQuoteData(NodeTreeCaseConditionData conditionData)
        {
            return dal.GetQuoteData(conditionData);
        }

        /// <summary>
        /// 获取法律法规信息(推送)
        /// </summary>
        /// <param name="ccId"></param>
        /// <returns></returns>
        public DataTable GetQuoteTuiSongData(string ccId)
        {
            return dal.GetQuoteTuiSongData(ccId);
        }

        /// <summary>
        /// 获取法规信息(推送)
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public DataTable GetQuoteData1(string condition)
        {
            return dal.GetQuoteData1(condition);
        }


        //// <summary>
        /// 获取法律法规条信息（查询）
        /// </summary>
        /// <param name="CATInID"></param>
        /// <returns></returns>
        public List<T_Law_Article> GetLawProvData(string catInId, string provCondition52)
        {
            return dal.GetLawProvData(catInId, provCondition52);
        }

        /// <summary>
        /// 根据条ID获取下级是否有款或者项信息
        /// </summary>
        /// <param name="provId"></param>
        /// <returns></returns>
        public bool GetNumByProvId(int provId)
        {
            return dal.GetNumByProvId(provId);
        }

        /// <summary>
        /// 获取法律法规款信息（查询）
        /// </summary>
        /// <param name="CATInID"></param>
        /// <returns></returns>
        public List<T_Law_Article> GetLawClauseData(int provInId)
        {
            return dal.GetLawClauseData(provInId);
        }

        /// <summary>
        /// 获取法律法规项信息（查询）
        /// </summary>
        /// <param name="CATInID"></param>
        /// <returns></returns>
        //public DataTable GetLawItemData(string ClauseNumber, string provInId)
        //{
        //    return dal.GetLawItemData(ClauseNumber, provInId);
        //}

        /// <summary>
        /// 获取法律法规项信息（查询）
        /// </summary>
        /// <param name="CATInID"></param>
        /// <returns></returns>
        public List<T_Law_Article> GetLawItemData(int provInId)
        {
            return dal.GetLawItemData(provInId);
        }

        /// <summary>
        /// 获取法律法规条款项信息（推送）
        /// </summary>
        /// <param name="CATInID"></param>
        /// <returns></returns>
        public DataTable GetLawProvDataByTuiSong(string CATInID, string ccID)
        {
            return dal.GetLawProvDataByTuiSong(CATInID, ccID);
        }

        /// <summary>
        /// 获取法律法规条款项界面信息（推送）
        /// </summary>
        /// <param name="CATInID"></param>
        /// <returns></returns>
        public DataTable GetTuiSongData(string catInIDs)
        {
            return dal.GetTuiSongData(catInIDs);
        }

        /// <summary>
        /// 根据条获取条信息
        /// </summary>
        /// <param name="ProvID"></param>
        /// <returns></returns>
        public string GetProvText(string ProvID)
        {
            return dal.GetProvText(ProvID);
        }

        /// <summary>
        /// 根据条获取条名称
        /// </summary>
        /// <param name="ProvID"></param>
        /// <returns></returns>
        //public string GetArticleName(string ProvID)
        //{
        //    return dal.GetArticleName(ProvID);
        //}

        /// <summary>
        /// 根据条和款获取款信息
        /// </summary>
        /// <param name="ProvID"></param>
        /// <returns></returns>
        public string GetClauseText(string ProvID, string clauseNumber)
        {
            return dal.GetClauseTextShow(ProvID, clauseNumber);
        }

        /// <summary>
        /// 根据条、款、项获取项信息
        /// </summary>
        /// <param name="ProvID"></param>
        /// <returns></returns>
        public string GetItemText(string ProvID, string clauseNumber, string itemNumber)
        {
            return dal.GetItemShow(ProvID, clauseNumber, itemNumber);
        }

        /// <summary>
        /// 根据条、款、项、xmlPath获取是否已存在记录
        /// </summary>
        /// <param name="ProvID"></param>
        /// <returns></returns>
        public bool IsExistsP(string ccName, string ccID, string ProvID, string clauseNumber, string itemNumber, string caseType, params string[] xmlPath)
        {
            return dal.IsExistsP(ccName, ccID, ProvID, clauseNumber, itemNumber, caseType, xmlPath);
        }

        /// <summary>
        /// 根据条获取是否已存在记录
        /// </summary>
        /// <param name="ProvID"></param>
        /// <returns></returns>
        public bool IsExistsProv(string ccName, string ccID, string ProvID)
        {
            return dal.IsExistsProv(ccName, ccID, ProvID);
        }

        /// <summary>
        /// 获取主题词关联法条数据
        /// </summary>
        /// <param name="headWordID"></param>
        /// <returns></returns>
        public DataTable GetData(string headWordID)
        {
            return dal.GetData(headWordID);
        }

        /// <summary>
        /// 插入引用法规主题词关联法条表
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public bool AddQuoteLawSet(QuoteLawSet Model)
        {
            return dal.AddQuoteLawSet(Model);
        }

        /// <summary>
        ///  修改引用法规主题词关联法条表
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public bool UpdateQuoteLawSet(QuoteLawSet Model)
        {
            return dal.UpdateQuoteLawSet(Model);
        }

        /// <summary>
        /// 判断引用法规主题词关联法条表是否已存在记录
        /// </summary>
        /// <param name="ProvID"></param>
        /// <returns></returns>
        public bool IsExistsQuoteLawSet(string ProvHwordInID)
        {
            return dal.IsExistsQuoteLawSet(ProvHwordInID);
        }

        /// <summary>
        /// 获得数据列表(组合)
        /// </summary>
        /// <param name="caseTypeId"></param>
        /// <returns></returns>
        public DataTable GetQuoteLawSet()
        {
            return dal.GetQuoteLawSet();
        }
    }
}
